package com.xywy.bloodandroid.act;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.adapter.QuestionDetailAdapter;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.QuestionDeatail;
import com.xywy.bloodandroid.bean.QuestionReplay;
import com.xywy.bloodandroid.receiver.BroadcastAction;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.JsonUtil;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.GetRequest;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;

/**
 * *****************************************
 * 
 * @Title: QuestionDetailAct.java
 * @Package com.xywy.maternalandchildandroid.act
 * @Description: TODO(问题详情)
 * @author smz
 * @date 2014-12-10 下午5:51:03
 * @version V1.0
 ******************************************* 
 */
public class QuestionDetailAct_Two extends BaseActivity {

	private RelativeLayout left_img;
	private TextView middle_title;
	private ListView question_listivew;
	private  List<QuestionDeatail> list;
	private String art_id;
	private QuestionDetailAdapter adapter;
	private String state;
	private FrameLayout      bottom_bar;
	private EditText  text_input;
	private ImageView send_control;
	private BroadcasrReciver receiver;
	
	private List<QuestionReplay> list2;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_question);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("问题详情");
		
	    bottom_bar = (FrameLayout) findViewById(R.id.bottom_bar);
	    text_input = (EditText) findViewById(R.id.text_input);
	    send_control = (ImageView) findViewById(R.id.send_control);
		
		list=new ArrayList<QuestionDeatail>();
		list2=new ArrayList<QuestionReplay>();
		
		art_id = getIntent().getExtras().getString("art_id");
	    state = getIntent().getExtras().getString("state");
	    adapter = new QuestionDetailAdapter(context, list,art_id,bottom_bar,text_input,send_control,list2);
	     
		question_listivew = (ListView) findViewById(R.id.question_listivew);
		question_listivew.setVisibility(View.VISIBLE);
		question_listivew.setAdapter(adapter);
		
		IntentFilter filter = new IntentFilter(
				BroadcastAction.ACTION_HIDE_SOFT_AND_REFESH_VIEWS);
		receiver = new BroadcasrReciver();
		registerReceiver(receiver, filter);
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
	}

	@Override
	protected void initData() {
     
     getQuestionDetail();
	}
   
	private void getQuestionDetail() {
		GetRequest<String> request=new GetRequest<String>(Constants.getUrl("club", "Club_quesDetail")+"&art_id="+art_id, String.class, new OnVolleyResponseListener<String>() {

			@Override
			public void onError(VolleyError volleyError) {
				UIUtil.DissDialog(context, waittingDialog);
				UIUtil.showToast(context, getString(R.string.network_slow));
			}

			@Override
			public void onSuccess(String response) {
				UIUtil.DissDialog(context, waittingDialog);
				JSONObject object;
				try {
					object = new JSONObject(response);
					int state=object.optInt("state");
					String data=object.optString("data");
					if(state==200){
						ParseJson(data);
					}else{
						UIUtil.showToast(context, object.getString("error"));
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
				
			}
		});
		request.setIsParseJson(false);
		executeRequest(request);
	}
    
	protected void ParseJson(String data) {
		Gson gson=new Gson();
		try {
			JSONObject object=new JSONObject(data);
			String ss=object.optString("repliedList");
			System.out.println("获取的医生回复。。。。"+ss);
			QuestionDeatail bean=gson.fromJson(data, QuestionDeatail.class);
			bean.setState(state);
//			bean.setRepliedlist(ss);
			list.add(bean);
			if(ss!=null&&ss.length()>0)
			list2.addAll((List<QuestionReplay>) JsonUtil.parseJsonToList(ss, 
				 new TypeToken<List<QuestionReplay>>() {
			}.getType()));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		adapter.notifyDataSetChanged();
		System.out.println("这是list的size "+list.size());
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;

		default:
			break;
		}
	}
	
	public class BroadcasrReciver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			System.out.println("获取的广播是什么呢.." + intent.getAction());
			if ("action_hide_soft_and_refesh_views".equals(intent.getAction())) {
				text_input.setText("");
				  bottom_bar.setVisibility(View.GONE);
					InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(text_input.getWindowToken(), 0);
			}
		}

	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);
	}
}
